<template>
	<view>

		<view class="list" v-for="(item, index) in data" :key="index">
			<view class="print" @click="connec(item)">连接打印机</view>
			<view>{{item.deviceName}}</view>
			<view>{{item.address}}</view>
		</view>

		<view class="but">
			<!-- <button @click="pairedDevices">获取已配对的蓝牙设备</button> -->

			<button class="margin-xs" @click="checkBlueth">搜索蓝牙</button>
			<button class="margin-xs" @click="cancelSearch">取消搜索</button>
			<button class="margin-xs" @click="disConnect">断开连接</button>
			<button class="margin-xs" @click="printerLable">打印测试</button>
			<button class="margin-xs" @click="checkBluethStatus">蓝牙状态</button>
			
		</view>
		<view class="text-center margin-top text-red">{{ tip }}</view>
	</view>
</template>

<script>
	const plug = uni.requireNativePlugin('Html5app-Gprinter');
	let _self ;
	export default {
		data() {
			return {
				tip: '',
				data: []
			}
		},
		onLoad() {
			_self = this 
		},
		methods: {
			pairedDevices() {
				let _this = this;
				//获取已配对的蓝牙设备
				plug.pairedDevices(function(e) {
					console.log(JSON.stringify(e));
					if (e.code == "0") {
						_this.data = e.list;
					} else {
						uni.showToast({
							title: "未有配对的设备",
							icon: "none"
						});
					}

				});

			},
			// 搜索之前检查蓝牙状态
			checkBlueth(){
				let _this = this;
				this.$utils.isOpenBlue(function(){
					_this.searchDevices()
				})
			},
			// 检查蓝牙状态
			checkBluethStatus(){
				let _this = this;
				this.$utils.isOpenBlue(function(){
					_self.$utils.tips('蓝牙已开启！')
				})
			},
			
			searchDevices() {
				let _this = this;
				//搜索蓝牙设备
				plug.searchDevices(function(e) {
					console.log(JSON.stringify(e));
					if (e.code == "0") {
						_this.data = e.list;
					} else {
						uni.showToast({
							title: e.msg,
							icon: "none"
						});
					}

				});


			},
			cancelSearch() {
				//取消搜索
				plug.cancelSearch();
			},
			connec(item) {
				let _this = this;
				//连接打印机
				plug.connectDevices({
					address: item.address
				}, ret => {
					let mm = item.deviceName +'打印机'+ ret.msg ;
					_this.tip = mm ;
					// console.log(mm);
					// console.log(JSON.stringify(ret));
					if(ret.state==3 || ret.code == 3){
						_self.cancelSearch()
						uni.setStorage({
							key: 'printMac',
							data: item.address
						});
						_self.$utils.tips('连接成功')
					}
				});

			},
			
			//打印标签
			printerLable(){
				let qrcode = {
					name: '打印物资测试',
					type: '物资类型',
					bcode: 'TM0020200602001',
					qcode:'0102|020102|01010101|TM0020200602001',
					number: 1
				}
				plug.printer({"TSC":_self.$utils.LableSet(qrcode)},ret=>{
					console.log(ret);
					// _this.tip=JSON.stringify(ret);
				});
			},
			
			disConnect(){
				plug.disconnect({},ret=>{
					// _this.tip=JSON.stringify(ret);
					console.log(ret);
				});
			}

		}

	}
</script>
<style>
	.list {
		margin: 15px;
		font-size: 14px;
		border-bottom: solid 1px #999999;
	}

	.but {
		margin: 10px;
		margin-top: 35px;
	}

	.print {
		float: right;
		height: 30px;
		line-height: 30px;
		text-align: center;
		width: 100px;
		background-color: #007AFF;
		color: #FFFFFF;
	}
</style>
